/**
 * @author: akolonitsky
 * @date: 12/7/11 2:59 PM
 */
package view.screens {
    import flash.utils.getAliasName;

    import model.AnalyticProxy;
    import model.LogProxy;
    import model.UserProxy;
    import model.vo.LogUpdateToken;
    import model.vo.ReportVO;
    import model.vo.UserVO;

    import mx.collections.ArrayCollection;
import mx.events.SliderEvent;
    import mx.resources.IResourceManager;
    import mx.resources.ResourceManager;

    import org.kolonitsky.alexey.debug.DebugWrapper;
    import org.kolonitsky.alexey.mvcext.NotificationMap;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    import view.controls.Report;

    [ResourceBundle("elfin_report")]

    public class AnalyticMediator extends Mediator
    {
        public static const NAME:String = "AnalyticMediator";

        public function AnalyticMediator(screen:AnalyticScreen)
        {
            super(NAME, screen);
            DebugWrapper.trace("INFO: " + getMediatorName() + " created.");
        }

        public function get screen():AnalyticScreen
        {
            return viewComponent as AnalyticScreen;
        }




        //-------------------------------------------------------------------------
        //
        // Mediator overriden methods
        //
        //-------------------------------------------------------------------------


        override public function onRegister():void
        {
            sendNotification(Config.LOG_GET, "1y");
            sendNotification(AppFacade.USER_AUTHORIZATION);
            screen.addEventListener(SliderEvent.CHANGE, selection_changeHandler);
        }

        override public function onRemove():void
        {
            screen.removeEventListener(SliderEvent.CHANGE, selection_changeHandler);
        }

        override public function listNotificationInterests():Array
        {
            return [
                UserProxy.USER_AUTHORIZATION,
                LogProxy.UPDATE,
                AnalyticProxy.REPORT_UPDATED,
                AnalyticProxy.NOT_ENOUGH_RECORDS
            ]
        }


        override public function handleNotification(notification:INotification):void
        {
            switch (notification.getName())
            {
                case UserProxy.USER_AUTHORIZATION:
                {
                    var user:UserVO = notification.getBody() as UserVO;
                    if (user.authorized)
                        screen.user = user;
                    break;
                }

                // This case for analytic page as default
                case LogProxy.UPDATE:
                {
                    var token:LogUpdateToken = notification.getBody() as LogUpdateToken;
                    if (token && token.type == LogUpdateToken.LOAD)
                    {
                        sendNotification(AppFacade.USER_AUTHORIZATION);
                        sendNotification(AppFacade.ANALYTIC_GET, localString("full-report-name"));
                    }
                    break;
                }

                case AnalyticProxy.REPORT_UPDATED:
                {
                    var report:ReportVO = notification.getBody() as ReportVO;
                    var mainReport:String = localString("full-report-name");
                    var dynamicReport:String = localString("dynamic-report-name");
                    switch (report.title)
                    {
                        case mainReport:
                        {
                            screen.report = report;
                            break;
                        }

                        case dynamicReport:
                        {
                            screen.dynamicReport = report;
                            break;
                        }
                    }
                    sendNotification(NotificationMap.PAGE_READY);
                    break;
                }

                case AnalyticProxy.NOT_ENOUGH_RECORDS:
                {
                    DebugWrapper.trace("WARNING: Analytic unavailable. Try to init AnalyticProxy with empty array.");
                    screen.isTimelineShow = false;
                    sendNotification(NotificationMap.PAGE_READY);
                    break;
                }
            }
        }




        //-------------------------------------------------------------------------
        //
        // Private logic
        //
        //-------------------------------------------------------------------------

        private function localString(key:String):String
        {
            return ResourceManager.getInstance().getString("elfin_report", key)
        }

        private function selection_changeHandler(event:SliderEvent):void
        {
            var report:ReportVO = new ReportVO(localString("dynamic-report-name"));
            report.beginDate = screen.beginDate;
            report.endDate = screen.endDate;
            sendNotification(AppFacade.ANALYTIC_GET, report);
        }
    }
}
